﻿using System;
using System.Web.UI.WebControls;
using BO.News;
using CBL;
using ChannelVN.Cached;
using System.Data;

namespace GUI
{
    public partial class cache : System.Web.UI.Page
    {
        private const string SessionName = "CommonCacheSession";
        protected void Page_Load(object sender, EventArgs e)
        {
            mvWrapper.SetActiveView(IsLoggedIn ? viewContent : viewLogin);
            if (!IsPostBack)
                ddlSites.Items.AddRange(new[]
                {
                    new ListItem("Kenh14","4"), 
                    new ListItem("Genk","2"), 
                    new ListItem("GameK","3"), 
                    new ListItem("CafeF","1"),
                    new ListItem("Afamily","5")
                });
        }


        private void ClearCache(string cacheKey)
        {
            if (
                CacheController.GetMemcachedInstanceForDataCached()
                               .Get(CacheController.CacheGroupForDataCached, cacheKey) != null)
            {
                CacheController.GetMemcachedInstanceForDataCached()
                               .Remove(CacheController.CacheGroupForDataCached, cacheKey);
                ltrMsg.Text = "Xóa cache thành công.";
            }
            else
            {
                ltrMsg.Text = "Cache đã được xóa hoặc không tồn tại.";
            }
        }


        protected void btnBoxNhungKenh14_Click(object sender, EventArgs e)
        {
            var cacheKey = NewsBO.CacheKeyManager.EmbedNews(4, 4);
            ClearCache(cacheKey);
        }

        protected void btnBoxNhungGenK_Click(object sender, EventArgs e)
        {
            var cacheKey = NewsBO.CacheKeyManager.EmbedNews(28, 2);
            ClearCache(cacheKey);
        }

        protected void btnBoxNhungGameK_Click(object sender, EventArgs e)
        {
            var cacheKey = NewsBO.CacheKeyManager.EmbedNews(28, 3);
            ClearCache(cacheKey);
        }

        protected void btnBoxNhungCafeF_Click(object sender, EventArgs e)
        {
            var cacheKey = NewsBO.CacheKeyManager.EmbedNews(28, 1);
            ClearCache(cacheKey);
        }

        protected void btnBoxNhungAf_Click(object sender, EventArgs e)
        {
            var cacheKey = NewsBO.CacheKeyManager.EmbedNews(3, 8);
            ClearCache(cacheKey);

        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            if (txtPassword.Text == "camvao")
            {
                Session[SessionName] = DateTime.Now.ToString("ddMMyyyy");
                mvWrapper.SetActiveView(viewContent);
            }
        }

        private bool IsLoggedIn
        {
            get
            {
                return Session[SessionName] != null && Session[SessionName].ToString() == DateTime.Now.ToString("ddMMyyyy");
            }
        }

        protected void btnLogout_Click(object sender, EventArgs e)
        {
            Session[SessionName] = null;
            mvWrapper.SetActiveView(viewLogin);
        }


        protected void btnView_Click(object sender, EventArgs e)
        {
            var siteId = ddlSites.SelectedValue.ChangeType<int>();
            var cacheKey = string.Empty;
            switch (siteId)
            {
                case 5:
                    {
                        cacheKey = NewsBO.CacheKeyManager.EmbedNews(3, 8);
                        break;
                    }
                case 4:
                    {
                        cacheKey = NewsBO.CacheKeyManager.EmbedNews(4, 4);
                        break;
                    }
                case 2:
                    {
                        cacheKey = NewsBO.CacheKeyManager.EmbedNews(28, 2);
                        break;
                    }
                case 3:
                    {
                        cacheKey = NewsBO.CacheKeyManager.EmbedNews(28, 3);
                        break;
                    }
                case 1:
                    {
                        cacheKey = NewsBO.CacheKeyManager.EmbedNews(28, 1);
                        break;
                    }
            }
            var cacheData =
                CacheController.GetMemcachedInstanceForDataCached()
                               .Get<DataTable>(CacheController.CacheGroupForDataCached, cacheKey);

            grvData.DataSource = cacheData;
            grvData.DataBind();


        }


    }
}